#ifndef TCP_SERVER_HTTPSERVER_H
#define TCP_SERVER_HTTPSERVER_H

#include "../TcpServer.h"

class HttpRequest;
class HttpResponse;

class HttpServer {
public:
    typedef std::function<void(const HttpRequest &, HttpResponse *)> HttpCallback;

    HttpServer(EventLoop *loop,
               const InetAddress &listenAddr,
               const string &name,
               TcpServer::Option option = TcpServer::kNoReusePort);

    EventLoop *getLoop() const { return server_.getLoop(); }

    /// Not thread safe, callback be registered before calling start().
    /**
     * 设置Http请求对应的回调函数, 参数体中的req为http请求封装, resp为我们要发送过去的http响应
     * @param cb http请求对应的处理函数
     */
    void setHttpCallback(const HttpCallback &cb) {
        httpCallback_ = cb;
    }

    void setThreadNum(int numThreads) { server_.setThreadNum(numThreads); }

    void start();

private:
    void onConnection(const TcpConnectionPtr &conn);

    void onMessage(const TcpConnectionPtr &conn,
                   Buffer *buf,
                   Timestamp receiveTime);

    void onRequest(const TcpConnectionPtr &, const HttpRequest &);

    TcpServer server_;
    HttpCallback httpCallback_;
};


#endif //TCP_SERVER_HTTPSERVER_H
